package com.htthorizon.system.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.htthorizon.common.core.constant.Constants;
import com.htthorizon.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.htthorizon.common.core.constant.CacheConstants;
import com.htthorizon.common.core.utils.poi.ExcelUtil;
import com.htthorizon.common.core.web.controller.BaseController;
import com.htthorizon.common.core.web.domain.AjaxResult;
import com.htthorizon.common.core.web.page.TableDataInfo;
import com.htthorizon.common.log.annotation.Log;
import com.htthorizon.common.log.enums.BusinessType;
import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.common.security.annotation.InnerAuth;
import com.htthorizon.common.security.annotation.RequiresPermissions;
import com.htthorizon.system.api.domain.SysLogininfor;
import com.htthorizon.system.service.ISysLogininforService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统访问记录
 * 
 * @author mark
 */
@RestController
@RequestMapping("/logininfor")
public class SysLogininforController extends BaseController
{
    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private RedisService redisService;

    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/list")
    @Log(title = "系统管理/登录日志查询", businessType = BusinessType.QUERY,type = Constants.SYSTEM)
    public TableDataInfo list(SysLogininfor logininfor)
    {
        startPage();
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    @Log(title = "系统管理/登录日志", businessType = BusinessType.EXPORT,type = Constants.SYSTEM)
    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor)
    {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PostMapping("/import")
    public R importExcel(@RequestPart("file") MultipartFile file) throws IOException {

        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        List<SysLogininfor> list = util.importExcel(file.getInputStream());
        logininforService.importExcel(list);
        return R.ok();
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "系统管理/登录日志", businessType = BusinessType.DELETE,type = Constants.SYSTEM)
    @PostMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds)
    {
        return toAjax(logininforService.deleteLogininforByIds(infoIds));
    }

//    @RequiresPermissions("system:logininfor:remove")
//    @Log(title = "登录日志", businessType = BusinessType.DELETE,type = Constants.SYSTEM)
//    @DeleteMapping("/clean")
//    public AjaxResult clean()
//    {
//        logininforService.cleanLogininfor();
//        return success();
//    }

    @RequiresPermissions("system:logininfor:unlock")
    @Log(title = "系统管理/账户解锁", businessType = BusinessType.OTHER,type = Constants.SYSTEM)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName)
    {
        redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName);
        return success();
    }

    @InnerAuth
    @PostMapping
    public AjaxResult add(@RequestBody SysLogininfor logininfor)
    {
        return toAjax(logininforService.insertLogininfor(logininfor));
    }

    @GetMapping("/count")
    public R count() {
        return R.ok(logininforService.count());
    }
}
